<template>
  <div class="flex flex-col h-full">
    <SearchContainer @search="queryList" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="flex-1" :model="formState" :colon="false" :label-col="{ style: { width: '70px' } }">
        <a-row :gutter="24">
          <a-col :span="8">
            <a-form-item name="pyjm">
              <a-input v-model:value="formState.pyjm" placeholder="护理等级名称/护理等级编号/助记码" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2 flex-1">
      <a-space class="mb-3">
        <a-button type="primary" @click="onClickEdit()">新增</a-button>
        <!-- <a-button @click="onExport">导出</a-button> -->
      </a-space>
      <BaseTable
        :loading="loading"
        column-code="00001063"
        fill
        :data-source="tableData"
        bordered
        :pagination="{ total: page.total, current: page.current, pageSize: page.pageSize }"
        @change="onPageChange"
      >
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'iconCode'">
            <span class="rounded p-1 w-6 text-white" :style="{ background: record.hlcolor }">{{ record.iconCode }}</span>
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-button type="link" size="small" @click="onClickEdit(record)"> 修改 </a-button>
            <a-divider type="vertical" />
            <ConfirmModal description="确定删除?" @confirm="onConfirm">
              <a-button type="text" danger size="small" @click="onClickDelete(record)"> 删除 </a-button>
            </ConfirmModal>
          </template>
        </template>
      </BaseTable>
    </a-card>
    <Edit v-model:open="editOpen" :row-data="rowData" @success="onSuceess" @confirm="editOpen = false" />
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'
import SearchContainer from '@/components/SearchContainer'
import BaseTable from '@/components/BaseTable'
import ConfirmModal from '@/components/ConfirmModal'
import Edit from './components/Edit.vue'

const rowData = ref(null)
const editOpen = ref(false)
const loading = ref(false)
const tableData = ref([])
const formRef = ref(null)
const formState = ref({})
const page = ref({ total: 0, current: 1, pageSize: getSystemConfig('DFFAULT_PAGE_SHOW_COUNT') || 10 })
const onClickEdit = (row = {}) => {
  editOpen.value = true
  rowData.value = JSON.parse(JSON.stringify(row))
}

const onConfirm = async () => {
  const { code, msg } = await post('/operation/opex/doDelNursingGradeInfo', { id: rowData.value.id })
  if (code !== 0) return message.error(msg || '删除失败')
  message.success('删除成功')
  queryList()
}
const onClickDelete = (row) => {
  rowData.value = row
}
const queryList = async () => {
  loading.value = true
  const { code, data, total } = await post('/operation/opex/getNursingGradeInfoList', {
    zjm: formState.value.pyjm,
    pageNum: page.value.current,
    pageSize: page.value.pageSize
  })
  if (code !== 0) return
  tableData.value = data
  tableData.value.forEach((item) => {
    switch (item.iconCode) {
      case 'Ⅰ':
        item.hlcolor = '#f5222d'
        break
      case 'Ⅱ':
        item.hlcolor = '#f97316'
        break
      case 'Ⅲ':
        item.hlcolor = '#22c55e'
        break
      case '特':
        item.hlcolor = '#fc03ca'
        break
    }
  })
  loading.value = false
  page.value.total = total
}
const onPageChange = ({ current, pageSize }) => {
  page.value.current = current
  page.value.pageSize = pageSize
  queryList()
}
const onSuceess = () => {
  editOpen.value = false
  queryList()
}
onMounted(() => {
  queryList()
})
</script>
<style lang="less" scoped>
.ant-picker {
  width: 100%;
}
</style>
